package com.yifang.yibingproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifang.yibingproject.dto.*;
import com.yifang.yibingproject.exception.ParamException;
import com.yifang.yibingproject.query.ForbiddenPageQuery;
import com.yifang.yibingproject.query.StaffPageQuery;
import com.yifang.yibingproject.service.SysUserService;
import com.yifang.yibingproject.utils.Log;
import com.yifang.yibingproject.utils.Result;
import com.yifang.yibingproject.utils.ResultUtils;
import com.yifang.yibingproject.utils.StringUtilOwn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "系统用户")
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("获取当前登录的用户信息")
    @GetMapping("getCurrentUserInfo")
    public Result<CurrentUserInfoDTO> getCurrentUserInfo(){
        return ResultUtils.data(sysUserService.getCurrentUserInfo());
    }


    @Log(value = "删除员工", resp = true)
    @ApiOperation("删除员工")
    @DeleteMapping("delStaff/{id}")
    public Result<String> delStaff(@ApiParam("员工id") @PathVariable String id) {

        return ResultUtils.data(sysUserService.delStaff(id));
    }

    @ApiOperation("编辑员工信息跳转页面")
    @PutMapping("updateStaffPage/{id}")
    public Result<StaffEditDTO> updateStaff(@ApiParam("员工用户编辑DTO") @PathVariable Long id) {
        return ResultUtils.data(sysUserService.updateStaffpage(id));
    }

    @Log(value = "新增员工", resp = true)
    @ApiOperation("新增员工")
    @PostMapping("addStaff")
    public Result<String> addStaff(@ApiParam("员工用户编辑DTO") @RequestBody @Valid StaffEditDTO dto) {
        dto.setId(null);
        dto.setUsername(dto.getUsername().trim());
        if (StringUtilOwn.isEmpty(dto.getUsername())) {
            throw new ParamException("用户名为空");
        }
        return ResultUtils.data(sysUserService.addStaff(dto));
    }


    @Log(value = "编辑员工", resp = true)
    @ApiOperation("编辑员工信息")
    @PutMapping("updateStaff")
    public Result<String> updateStaff(@ApiParam("员工用户编辑DTO") @RequestBody @Valid StaffEditDTO dto) {
        if (StringUtilOwn.isEmpty(dto.getUsername())) {
            throw new ParamException("用户名为空");
        }
        return ResultUtils.data(sysUserService.updateStaff(dto));
    }

    @ApiOperation("获取用户详细信息")
    @GetMapping("getUserDetail/{id}")
    public Result<UserDetailDTO> getUserDetail(@ApiParam("用户id") @PathVariable String id) {
        return ResultUtils.data(sysUserService.getUserDetail(id));
    }

    @Log(value = "禁用账号", resp = true)
    @ApiOperation("禁用账号")
    @GetMapping("forbidden")
    public Result<Boolean> forbidden(@ApiParam("用户id") @RequestParam String id, @ApiParam("禁用理由") @RequestParam String reason) {
        return ResultUtils.data(sysUserService.forbidden(id, reason));
    }


    @Log(value = "解禁账号", resp = true)
    @ApiOperation("解禁账号")
    @GetMapping("liftBan")
    public Result<Boolean> liftBan(@ApiParam("用户id") @RequestParam String id) {
        return ResultUtils.data(sysUserService.liftBan(id));
    }

    @ApiOperation("禁用账号列表")
    @GetMapping("forbiddenList")
    public Result<Page<ForbiddenDTO>> forbiddenList(@ApiParam("用户分页查询对象") @ModelAttribute ForbiddenPageQuery query) {
        return ResultUtils.page(sysUserService.forbiddenList(query));
    }


    @Log(value = "清除/保存操作权限", resp = true)
    @ApiOperation("保存操作权限")
    @PostMapping("saveActionRight")
    public Result<Boolean> saveActionRight(@ApiParam("操作权限编辑DTO") @RequestBody @Valid ActionRightEditDTO dto) {
        if (dto.getUserTypeId().equals("0")) {
            return ResultUtils.data(sysUserService.clearUserType(dto.getUserId()));
        } else {
            return ResultUtils.data(sysUserService.saveActionRight(dto));
        }
    }

    @ApiOperation("员工列表")
    @GetMapping("staffList")
    public Result<Page<StaffDTO>> staffList(@ApiParam("用户分页查询对象") StaffPageQuery query) {
        return ResultUtils.page(sysUserService.staffList(query));
    }
}
